<template>
    <div>
        <a-modal v-model="show" title="设置密码" @ok="handleOk" @cancel="show = false">
            <a-form :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 19 }">
                <a-form-item label="手机号">
                    <a-input v-model="form.mobile" />
                </a-form-item>
                <a-form-item label="设置密码">
                    <a-input v-model="form.password" type="password" />
                </a-form-item>
                <a-form-item label="确认密码">
                    <a-input v-model="form.password1" type="password" />
                </a-form-item>

            </a-form>
        </a-modal>
    </div>
</template>
<script>
    import { message } from 'ant-design-vue';
    import { postUserPassword } from "@/services"
    export default {
        props: {
            // 组织列表
            orgList: {
                default: () => { },
                type: Array,
            },
        },
        data() {
            return {
                // 是否显示弹框
                show: false,
                // 表单
                form: {
                    mobile: '',
                    password: '',
                    userId: '',
                    password1: '',
                }
            }
        },
        methods: {
            /**
             * 打开弹框
             */
            open(data) {
                this.show = true // 打开弹框
                this.form = this.$options.data().form // 初始化
                if (data?.userId) {
                    this.form.mobile = data.mobile
                    this.form.userId = data.userId
                }
            },
            /**
             * 确认提交
             */
            async handleOk() {
                let { form } = this
                if (form.mobile == '') {
                    return message.warning('请填写手机号')
                }
                if (!/^1[23456789]\d{9}$/.test(form.mobile)) {
                    return message.warning('手机号格式不正确')
                }
                if (form.password == '') {
                    return message.warning('请填写密码')
                }
                if (form.password1 == '') {
                    return message.warning('请确认密码')
                } 
                if (form.password !== form.password1) {
                    return message.warning('密码不一致')
                }
                let res = await postUserPassword(this.form)
                if (res.code == 0) {
                    message.success('修改成功')
                } else {
                    return message.warning(res?.msg)
                }
                this.show = false
                this.$emit('ok', 1);
            }
        },
    }
</script>